﻿Imports System.IO
Imports System.Data.OleDb

Public Class ctrDangNhap

    Structure Account
        Dim username As String
        Dim password As String
        Dim acctype As String
    End Structure

    Public lsAcc As List(Of Account)

    Private Sub btnDangNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDangNhap.Click
        For i = 0 To lsAcc.Count() - 1 Step 1
            If (txtTenDN.Text = lsAcc(i).username And txtMatKhau.Text = lsAcc(i).password) Then
                frmMain.Panel1.Enabled = True
                frmMain.lbUser.Text = lsAcc(i).username
                frmMain.lbType.Text = "(" + lsAcc(i).acctype + ")"
                If (lsAcc(i).acctype = "Quản lý") Then
                    frmMain.btnDuLieu.Enabled = False
                    frmMain.btnHeThong.Enabled = False
                ElseIf (lsAcc(i).acctype = "Nhân viên") Then
                    frmMain.btnDuLieu.Enabled = False
                    frmMain.btnHeThong.Enabled = False
                ElseIf (lsAcc(i).acctype = "Admin") Then
                    frmMain.btnDuLieu.Enabled = True
                    frmMain.btnHeThong.Enabled = True
                End If
                frmMain.TabControl1.TabPages.Remove(frmMain.TabControl1.SelectedTab)
                frmMain.btnDangNhap.Text = "Đăng xuất"
            End If
        Next
        lbThongBao.Text = "Vui lòng kiểm tra lại tên đăng nhập và mật khẩu!!!"
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        frmMain.Close()
    End Sub

    Private Sub ctrDangNhap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lsAcc = New List(Of Account)()

        Dim cn As OleDbConnection = DataProvider.ConnectionData()
        Dim dt As DataTable = New DataTable()
        Dim str As String = "select TenDangNhap, MatKhau, LoaiTK from TAIKHOAN"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(str, cn)
        da.Fill(dt)
        cn.Close()

        Dim tmp As Account
        For i = 0 To dt.Rows.Count() - 1 Step 1
            tmp = New Account()
            tmp.username = dt.Rows(i)(0).ToString()
            tmp.password = dt.Rows(i)(1).ToString()
            tmp.acctype = dt.Rows(i)(2).ToString()
            lsAcc.Add(tmp)
        Next

        'txtTenDN.Text = "admin"
        'txtMatKhau.Text = "12345"
    End Sub
End Class
